package com.mes.web.controller.biz;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.mes.biz.domain.BizOrder;
import com.mes.biz.domain.BizOrderProduct;
import com.mes.biz.domain.BizProduct;
import com.mes.biz.domain.vo.BizOrderMaterialVo;
import com.mes.biz.service.BizProductService;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.AjaxResult;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.model.LoginUser;
import com.mes.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.mes.framework.web.service.TokenService;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.domain.BizOrderMaterial;
import com.mes.biz.service.BizOrderMaterialService;

/**
 * 销售单物料Controller
 * @author wy
 * @email wy
 * @date 2023-06-20
 */
@RestController
@RequestMapping("/biz/bizOrderMaterial")
public class BizOrderMaterialController extends BaseController {

    @Autowired
    private BizOrderMaterialService bizOrderMaterialService;
    @Autowired
    private BizProductService bizProductService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询销售单物料列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderMaterial:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizOrderMaterial bizOrderMaterial, HttpServletRequest request, HttpServletResponse response) {
        bizOrderMaterial.setPage(new PageDomain(request, response));
        PageInfo<BizOrderMaterial> page = bizOrderMaterialService.findPage(bizOrderMaterial);
        for (int i = 0; i < page.getList().size(); i++) {
            BizProduct bizProduct = bizProductService.get(page.getList().get(i).getProductId());
            page.getList().get(i).setProductCode(bizProduct.getProductCode());
            page.getList().get(i).setModel(bizProduct.getModel());
            page.getList().get(i).setBrand(bizProduct.getBrand());
            page.getList().get(i).setColor(bizProduct.getColor());
            page.getList().get(i).setSpec(bizProduct.getSpec());
        }
        return R.data(page);
    }

    /**
     * 获取销售单物料详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderMaterial:query')")
    @GetMapping(value = "/{id}")
    public R<BizOrderMaterial> detail(@PathVariable("id") String id) {
        return R.data(bizOrderMaterialService.get(id));
    }

    /**
     * 新增销售单物料
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderMaterial:add')")
    @Log(title = "销售单物料", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizOrderMaterial bizOrderMaterial) {
        return R.status(bizOrderMaterialService.save(bizOrderMaterial));
    }

    /**
     * 修改销售单物料
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderMaterial:edit')")
    @Log(title = "销售单物料", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizOrderMaterial bizOrderMaterial) {
        return R.status(bizOrderMaterialService.save(bizOrderMaterial));
    }

    /**
     * 删除销售单物料
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderMaterial:remove')")
    @Log(title = "销售单物料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizOrderMaterialService.deleteBizOrderMaterialByIds(ids));
    }


    /**
     * 导出销售单物料列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderMaterial:export')")
    @Log(title = "销售单物料", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizOrderMaterial bizOrderMaterial) {
        List<BizOrderMaterial> list = bizOrderMaterialService.findList(bizOrderMaterial);
        ExcelUtil<BizOrderMaterial> util = new ExcelUtil<BizOrderMaterial>(BizOrderMaterial.class);
        return util.exportExcel(list, "销售单物料数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    /**
    * @Author: wy
    * @Description: 领料
    * @DateTime: 2023-06-25 16:29
    * @Params:
    * @Return
    */
//    @PostMapping("/saveMaterials")
//    public R getMaterials(@RequestBody  List<BizOrderMaterial> bizOrderMaterial,String orderId) {
//        return R.status(bizOrderMaterialService.saveMaterials(bizOrderMaterial,orderId));
//    }

    @PostMapping("/saveMaterials")
    public R getMaterials(@RequestBody BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return R.status(bizOrderMaterialService.saveMaterials(bizOrder,loginUser));
    }


    /**
    * @Author: wy
    * @Description: 领料详情列表
    * @DateTime: 2023-06-28 17:11
    * @Params:
    * @Return
    */
    @GetMapping("/detailList/{orderId}")
    public AjaxResult detailList(@PathVariable("orderId") String orderId, HttpServletRequest request, HttpServletResponse response) {
        List<BizOrderMaterial> list = bizOrderMaterialService.getBizOrderMaterialByOrderId(orderId);
        return AjaxResult.success(list);
    }

    /**
     * 查询领料明细列表
     */
    @GetMapping("/findPageExportList")
    public R<PageInfo> findPageExportList(BizOrderMaterial bizOrderMaterial, HttpServletRequest request, HttpServletResponse response) {
        bizOrderMaterial.setPage(new PageDomain(request, response));
        PageInfo<BizOrderMaterialVo> page = bizOrderMaterialService.findPageExportList(bizOrderMaterial);
        return R.data(page);
    }

    @GetMapping("/findPagePickList")
    public R<PageInfo> findPagePickList(BizOrderMaterial bizOrderMaterial, HttpServletRequest request, HttpServletResponse response) {
        bizOrderMaterial.setPage(new PageDomain(request, response));
        PageInfo<BizOrderMaterialVo> page = bizOrderMaterialService.findPagePickList(bizOrderMaterial);
        return R.data(page);
    }

    /**
     * 打印领料明细
     */
    @GetMapping("/findExportList")
    public void findExportList(BizOrderMaterial bizOrderMaterial,HttpServletResponse response) {
        List<BizOrderMaterialVo> list = bizOrderMaterialService.findExportList(bizOrderMaterial);
        ExcelUtil<BizOrderMaterialVo> util = new ExcelUtil<>(BizOrderMaterialVo.class);
        if (bizOrderMaterial.getProductType().equals("2")){
            util.printExcel(response,list, "领料明细表");
        }else{
            util.printExcel(response,list, "退料明细表");
        }
    }

    @GetMapping("/findExportPickList")
    public void findPickList(BizOrderMaterial bizOrderMaterial,HttpServletResponse response) {
        List<BizOrderMaterialVo> list = bizOrderMaterialService.findPickList(bizOrderMaterial);
        ExcelUtil<BizOrderMaterialVo> util = new ExcelUtil<>(BizOrderMaterialVo.class);
        util.printExcel(response,list, "领料明细表");
    }


}
